package com.xyzwps.server.paimon.graphql.scalar

import com.xyzwps.server.paimon.value.VcodeScene
import com.xyzwps.server.paimon.value.toVcodeScene
import graphql.GraphQLContext
import graphql.execution.CoercedVariables
import graphql.language.IntValue
import graphql.language.NullValue
import graphql.language.Value
import graphql.schema.Coercing
import graphql.schema.CoercingParseLiteralException
import graphql.schema.CoercingParseValueException
import graphql.schema.GraphQLScalarType
import java.util.Locale
import kotlin.jvm.javaClass

internal object VCodeSceneCoercing : Coercing<VcodeScene, Int> {
    override fun serialize(dataFetcherResult: Any, ctx: GraphQLContext, locale: Locale): Int =
        when (dataFetcherResult) {
            is VcodeScene -> dataFetcherResult.value
            else -> throw CoercingParseValueException("Expected type ${VcodeScene::class.simpleName} but was ${dataFetcherResult.javaClass.simpleName}")
        }

    override fun parseValue(input: Any, ctx: GraphQLContext, locale: Locale): VcodeScene =
        when (input) {
            is Int -> input.toVcodeScene()
            else -> throw CoercingParseValueException("Expected type Int but was ${input.javaClass.simpleName}")
        }

    override fun parseLiteral(input: Value<*>, vars: CoercedVariables, ctx: GraphQLContext, locale: Locale): VcodeScene? =
        when (input) {
            is IntValue -> input.value.toInt().toVcodeScene()
            is NullValue -> null
            else -> throw CoercingParseLiteralException("Expected type IntValue but was ${input.javaClass.simpleName}")
        }
}

val VCODE_SCENE_SCALAR: GraphQLScalarType = GraphQLScalarType.newScalar()
    .name("VCodeScene")
    .description("VCode scena")
    .coercing(VCodeSceneCoercing)
    .build()